//
//  HomeShowViewController.m
//  Mahjong
//
//  Created by KING on 16/9/12.
//  Copyright © 2016年 swift. All rights reserved.
//

#import "HomeShowViewController.h"
#import "MineShowTableViewCell.h"
#import "GoodsListViewController.h"
#import "ShowDetailViewController.h"
#import "Header.h"
#import "HomeShowModel.h"
@interface HomeShowViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *dateArray;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL updata;
@end

@implementation HomeShowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [self.view addSubview:self.myTableView];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.myTableView registerNib:[UINib nibWithNibName:@"MineShowTableViewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
    
    
    self.navigationItem.title = @"促销";
    self.dateArray = [NSMutableArray array];

    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    [self create];
    
//    self.page = 1;
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(header)];
    [self.myTableView.mj_header beginRefreshing];
//    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footer)];

}
-(void)header{
    [self create];
}
-(void)create{
    [AppTools createAFN:@"http://api03.m0571.com/WebJson.aspx?book=GetPromotion" success:^(id result) {
        [self.dateArray removeAllObjects];
        [self.myTableView.mj_header endRefreshing];
        for (NSDictionary *dic in [result[0] objectForKey:@"date"]) {
            HomeShowModel *model = [[HomeShowModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dateArray addObject:model];
        }
        [self.myTableView reloadData];
        
    } error:^(NSError *error) {
        
        
    }];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dateArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    MineShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    HomeShowModel *model = self.dateArray[indexPath.row];
    cell.headLabel.text = model.PromotionTitle;
    return cell;
    
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 170;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShowDetailViewController *list = [[ShowDetailViewController alloc] init];
    HomeShowModel *model = self.dateArray[indexPath.row];
    list.value = model.PromotionValue;
    list.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:list animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
